﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Corona.TagToolkit;
using System.IO;
namespace Corona.TagPresent
{
    class File
    {
        private string filePath;
        private string fileName;
        private string fileCreatedTime;
        private string fileLastModifiedTime;
        private string fileLastAccessTime;
        private System.IO.FileAttributes fileAttributes;
        private string fileType;
        private string md5;

        public string Md5
        {
            get { return md5; }
        }

        public string FileType
        {
            get { return fileType; }
        }

        public System.IO.FileAttributes FileAttributes
        {
            get { return fileAttributes; }
        }

        public string FileCreatedTime
        {
            get { return fileCreatedTime; }
        }
        public string FileLastModifiedTime
        {
            get { return fileLastModifiedTime; }
        }
        public string FileLastAccessTime
        {
            get { return fileLastAccessTime; }
        }
        public string FileName
        {
            get { return fileName; }
        }

        public string FilePath
        {
            get { return filePath; }
            set
            {
                filePath = value;
                md5 = getFileMD5(value);
                fileName = value.Substring(value.LastIndexOf('\\') + 1);
                fileType = value.Substring(value.LastIndexOf('.') + 1);
                fileCreatedTime = System.IO.File.GetCreationTime(value).ToShortDateString();
                fileLastModifiedTime = System.IO.File.GetLastWriteTime(value).ToShortDateString();
                fileLastAccessTime = System.IO.File.GetLastAccessTime(value).ToShortDateString();
                fileAttributes = System.IO.File.GetAttributes(value);
            }
        }

        public File(string path)
        {
            this.filePath = path;
        }

        private String getFileMD5(String path)
        {
            try
            {
                FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] hash_byte = md5.ComputeHash(file);
                string result = System.BitConverter.ToString(hash_byte);
                result = result.Replace("-", "");
                return result;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

    }
    class FileResource: Resource<File>, IControllable
    {

        #region IControllable Members

        public FileResource(string locazier)
            : base(locazier)
        {
        }

        public List<Corona.TagToolkit.Tag> analyseTags()
        {
            List<Corona.TagToolkit.Tag> tagList = new List<Corona.TagToolkit.Tag>();
            tagList.Add(new Corona.TagToolkit.Tag("MD5",this.Value.Md5));
            tagList.Add(new Corona.TagToolkit.Tag("FileName",this.Value.FileName));
            tagList.Add(new Corona.TagToolkit.Tag("FilePath", this.Value.FilePath));
            tagList.Add(new Corona.TagToolkit.Tag("FileType", this.Value.FileType));

            if((this.Value.FileAttributes & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden)
                tagList.Add(new Corona.TagToolkit.Tag("FileAttribute","隐藏"));
            if ((this.Value.FileAttributes & System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly)
                tagList.Add(new Corona.TagToolkit.Tag("FileAttribute", "只读"));
            if ((this.Value.FileAttributes & System.IO.FileAttributes.System) == System.IO.FileAttributes.System)
                tagList.Add(new Corona.TagToolkit.Tag("FileAttribute", "系统"));

            tagList.Add(new Corona.TagToolkit.Tag("CreatedTime", this.Value.FileCreatedTime));
            tagList.Add(new Corona.TagToolkit.Tag("LastModifiedTime", this.Value.FileLastModifiedTime));
            tagList.Add(new Corona.TagToolkit.Tag("LastAccessTime", this.Value.FileLastAccessTime));
            return tagList;
            
        }

        #endregion
    }
}
